package net.xdclass.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.KeyFactory;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

public class RSAUtils {
    private static int maxDecryptCipherTextLen = 128;

    public RSAUtils() {
    }
    public static byte[] decryptBASE64(String key) {
        return Base64.getDecoder().decode(key);
    }

    public static PrivateKey getPrivateKeyByStr(String privateKeyStr) throws NoSuchAlgorithmException, InvalidKeySpecException {
        byte[] privateKeyBytes = decryptBASE64(privateKeyStr);
        PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(pkcs8EncodedKeySpec);
    }

    public static String decrypt(Map<String, Object> keyPair, String cipherText, String key) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        ByteArrayOutputStream plainText = null;

        String var15;
        try {
            byte[] cipherTextBytes = decryptBASE64(cipherText);
            int cipherTextBytesLen = cipherTextBytes.length;
            int decryptCount = (int)Math.ceil((double)cipherTextBytesLen * 1.0 / (double)maxDecryptCipherTextLen);
            plainText = new ByteArrayOutputStream();
            Cipher rsaCipher = Cipher.getInstance("RSA");
            rsaCipher.init(2, (Key)keyPair.get(key));

            for(int i = 0; i < decryptCount; ++i) {
                int offSet = i * maxDecryptCipherTextLen;
                byte[] plainTextBytes;
                if (offSet + maxDecryptCipherTextLen < cipherTextBytesLen) {
                    plainTextBytes = rsaCipher.doFinal(cipherTextBytes, offSet, maxDecryptCipherTextLen);
                } else {
                    plainTextBytes = rsaCipher.doFinal(cipherTextBytes, offSet, cipherTextBytesLen - offSet);
                }

                plainText.write(plainTextBytes, 0, plainTextBytes.length);
            }

            var15 = plainText.toString();
        } finally {
            if (plainText != null) {
                plainText.close();
            }

        }

        return var15;
    }

    public static String decryptByPrivate(Map<String, Object> keyPair, String cipherText) throws IOException, NoSuchPaddingException, NoSuchAlgorithmException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
        return decrypt(keyPair, cipherText, "RSAPrivateKey");
    }

    public static String decryptByPrivateStr(String privateKeyStr, String cipherText) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, IllegalBlockSizeException, IOException, BadPaddingException, InvalidKeyException {
        PrivateKey privateKey = getPrivateKeyByStr(privateKeyStr);
        HashMap<String, Object> keyPair = new HashMap();
        keyPair.put("RSAPrivateKey", privateKey);
        return decryptByPrivate(keyPair, cipherText);
    }

    public static void main(String[] args) throws Exception{
        String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJbZ5xVPdvlqva+G7kEHTov7fo7VM/WxQju+yXAZaLL6ME4HqJ4aCo5yI/bJLIdXJ92WRLMOqwKqpm7/M9KTF+n4FQ2R+hU6FRBoDDLmF92xfrmfn5iwyl9xjS9cmCsXDhgJsyIoF1ak1nMSpg9TqaVH8ach4AEz7Cv1s9RSE7IdAgMBAAECgYBdjZ3mdXXPkQc6iG/SHHn2Dm6anlavFNOCwfujbd6kwMycV9uW0Yeid98d+f2HoeGzZrIMahhcewDk7ZhyC49ULbgCopZdawM8qVNUfKg+Nrtpcip0i0AmFXut4FtGADcehpNgl+saN/YZQZNxeS+lPn/9oJ5lwBCfCPfTLuZaAQJBAPKyo8OAVvVzQcm+ErIX3yKAnsyVNHHdlJ8uwWf5D+lpmmN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Zncrrg==";

        //String privateKey = "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAKKumLZFoR2la5I8JmlQa0ikect1Ih4SSY6qDupTg7tdI227FkmeNA4hOB8BrFawwnzX02i8LBNu0cS0Zv8b8G8LIsmJvMQil5hol8om3YlW4DBhHvYAdUzYpQGiT/lTr41inapgdMQpsfZekNKrMJR+unvz88MQD7YN5i2DMMGTAgMBAAECgYEAj8WcAuJQfIHYKrnr/IDs/5sUZIIVb9wbk/CpE+BQ+IJcsDp6fP9K4/3pukVhSJAYofqfxN0gWuBClQQWTnkjp6II9nrNAwKrmJuWyFWcv3V5Zk9YnaRZrCdvAXG8x3mXMGict2TyL3yTd1rpOutNPlik/bARYLkPYvRpj8CwLFkCQQDwA5UlYLPIEM0Sfo6AAIEMUfIV0KyQhtivJBztZVDliF4XgvQ26K3jfRa15gwTmYNmsofsO6Ze8jHvh7ZdPaM/AkEArYRyNM/XX3F639u75BWAZf2xmDIj/KzPhJblggLKPcuzRkFNYTEoVdTRu/rRUkwuTA11hGf4Xdw/E0Jku8qQrQJAeuQatayx4LpZhiKuq/OWH8zFe04BTW79+48uEI9TwHL2Wmt+x66TemtN+Yz0mgR8N1UqK1bPdMXls5N1WIB62QJAFbqjTqfEcchVOdlbgymKJbuKVOPai9bcOd9nqEtPJkPvFFozJXuxcGYxSZgURHkTxyRcVhNrD9337CKBWN76FQJANEvWhua7DwBHLxWIoh7V/+RM+bHYCcGCkPKyXW6AwDKyZ2uq9lVQpSnPAr9GZydUsdRa2f+bl9y1XFU6B0hyMA==";
        //String response = "LK8HZkNkBLjAmK0puKT51LiREy68HAgReAQ6OEqrWyEz/zFRQ8cKAugf939+mNdiSmKpkgMn2m7sWjKcVxSxQofRRhIkjiqf/c+YRi0I9HqE/0oXnXeA+naqaxpHI0WmTw5x3e8W5XH4VMRmwsQPFAv1RLmyrxAdWbeZjMG/CkFy+WqF5X64PAeQ8XkKddvCMIFGtlEQCc9c8WhJ7nVYifSDXjSfifDvT1mYVxJciqpR9qWXb4HnL9UrfBORNz9gQqV+fU3srAJrk/t4BhagBa5S1sU8gu7lsoCyt73P+l/NyUSdyt8cYNuEsTwDn0nv3jsSoZsjpbuEjDMzs+9nilD949qttJxdVH+kRiTu4xuUhujdSCvW60XRViOsMEgJAmb57c5hHVGi4RX2Lm4P7ba03djux+5n7A3ybqsJjd1KjHHbMc9qtiMYYpNkt/PHOKcf0f1sIZzXSTXKZ1+tyIbsy8PZcGfpYtNiPts21gKA2KPL2ubLNVnE0X/ssu4hFB4276Z4SWZ/IH5DqQTDFCGnkpLrHLW4rPhqk9zZZMLf1FgDiAOiUZzCtbeRefgK2LRuGT44sEUl0+7kQ+MQ9jWk33FgqN7AMFIksUl8S21tOSpVgJaA+2RMOHR2h484M66/Of3mNulHfz9yfphbMiTuI5BeNUAVE8h85QE/QLJzHfY2uKOAAy3ru0eP3Sfc6Gsl69Mc+KLv4Xr5aT6ErkdgSTy3s1b0EZ0x4IAqWTPXx2MQlRivHtZ+TAlYRmU6OTJhMhyPeuozahWbYNIcwg8ua8Z21dk7uEMkfgGBDSQxXRB117kyhmb1GJAdp5nGvEwhuqMnS4FOUL7gX+KgvDLYCAru6hkS/VHSCyfq3S1BdtU2tOPNtni9nl7+LLFIA0Ry9MbrX9YSj5sxl3bvmLfUkzsK+Pvp6bTAZBT7qeL9QPS2snkfk9fr5J5LlEwxuUZZMpy78xsjsmwRpd3szxjWK6av41WdCgldZEY9YCqFzwKrwiQdtPXPaKEfrZyE";
        response = RSAUtils.decryptByPrivateStr(privateKey,response);
        System.out.println(response);
    }
}

